#include "stm32f10x.h"                  // Device header
#define Max_PWM 7200
#define Min_PWM -7200

/**
   *@brief     占空比最大为7200,最小为-7200,此函数用于把PID计算的值合理化
   *@parem  	 电机A的占空比,电机B的占空比
   *@retval    无
   */
void Limid(int16_t *MotorA,int16_t *MotorB)
{
	/*
	if(*MotorA > 0) *MotorA += 700;
	else 						*MotorA -= 700;
	if(*MotorB > 0) *MotorB += 700;
	else 						*MotorB -= 700;*/
	if(*MotorA > Max_PWM) *MotorA = Max_PWM;
	if(*MotorA < Min_PWM) *MotorA = Min_PWM;
	if(*MotorB > Max_PWM) *MotorB = Max_PWM;
	if(*MotorB < Min_PWM) *MotorB = Min_PWM;
}
//绝对值函数
uint16_t ads(int16_t Motor)
{
	return (Motor >= 0 ? Motor : -Motor);
}
/**
   *@brief     给两个电机装载速度和方向
   *@parem  	 Motor1电机1的速度和方向,Motor2电机2的速度和方向
   *@retval    无
   */

#define N 3100

void Load_SpeedsAndDirections(int16_t Motor1,int16_t Motor2)
{
	if(Motor1 > 0)
	{
		TIM_SetCompare1(TIM1,N + ads(Motor1));
		TIM_SetCompare2(TIM1,0);
	}
	else
	{
		TIM_SetCompare2(TIM1,N + ads(Motor1));
		TIM_SetCompare1(TIM1,0);
	}
	
	if(Motor2 > 0)
	{
		TIM_SetCompare3(TIM1,N + ads(Motor2));
		TIM_SetCompare4(TIM1,0);
	}
	else
	{
		TIM_SetCompare4(TIM1,N + ads(Motor2));
		TIM_SetCompare3(TIM1,0);
	}
}